/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.httpclient5.interceptors;

import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpRequestInterceptor;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.protocol.HttpContext;

import io.gitee.huangjianbo.supos.openapi.core.config.SuposApiClientConfig;
import io.gitee.huangjianbo.supos.openapi.core.headers.CommonHeaders;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpHeader;

import java.io.IOException;

/**
 * 自定义请求头拦截器
 *
 * @author tomcat
 * @version 1.0
 */
public class CustomHeadersInterceptor implements HttpRequestInterceptor {

    private final SuposApiClientConfig globalConfig;

    public CustomHeadersInterceptor(SuposApiClientConfig globalConfig) {
        this.globalConfig = globalConfig;
    }

    @Override
    public void process(HttpRequest request, EntityDetails entity, HttpContext context) throws HttpException, IOException {
        HttpHeader httpHeader = CommonHeaders.getRequestHeaders(globalConfig);
        httpHeader.toMap().forEach((k, v) -> request.addHeader(new BasicHeader(k, v)));
    }
}
